package com.example.springsecurity.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.domain.LearnMaterials;
import com.example.springsecurity.domain.Project;
import com.example.springsecurity.domain.SysUser;
import com.example.springsecurity.domain.Video;
import com.example.springsecurity.security.SecurityUtils;
import com.example.springsecurity.service.ExperimentService;
import com.example.springsecurity.utils.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 项目实验控制层（项目）
 */
@Controller
@RequestMapping("/experiment")
public class ExperimentController {


    @Autowired
    public ExperimentService experimentService;


    private Logger logger= LoggerFactory.getLogger(ExperimentController.class);

    /**
     * 项目添加人添加项目
     * @param project
     * @param file
     * @return
     */
//    @PreAuthorize("@ss.hasAnyPermi('sys:experiment:add')")
    @ResponseBody
    @RequestMapping(value = "/addExperiment",method = RequestMethod.POST)
    public ResultBean addExperiment(Project project, MultipartFile file){
        return experimentService.addExperiment(project,file);
    }


    /**
     * 项目添加人查询项目信息（列表）
     * @param experiment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchExperiment",method = RequestMethod.POST)
    public ResultBean searchExperiment(@RequestBody String experiment){
        JSONObject jsonObject=JSONObject.parseObject(experiment);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String projectName=jsonObject.getString("projectName");
        return experimentService.searchExperiment(pageNum,pageSize,projectName);
    }


    /**
     * 项目添加人编辑修改项目信息
     * @param project
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateProject",method = RequestMethod.POST)
    public ResultBean updateProject( Project project,MultipartFile file){
        return experimentService.updateProject(project,file);

    }


    /**
     * 项目添加人根据项目编号来获取到项目的详细信息
     * @param getProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProject",method = RequestMethod.POST)
    public ResultBean getProject(@RequestBody String getProject){
        JSONObject jsonObject= JSON.parseObject(getProject);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.getProject(projectId);
    }


    /**
     * 下载项目的客户端实验文件
     * @param projectId
     * @param request
     * @param response
     */
    @RequestMapping(value = "/downloadProjectFile",method = RequestMethod.GET)
    public void downloadProjectFile(Integer projectId,HttpServletRequest request,HttpServletResponse response){
        experimentService.downloadProjectFile(projectId,request,response);
    }


    /**
     * 项目添加人根据项目编号来删除未被布置的项目
     * @param deleteProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteProject",method = RequestMethod.POST)
    public ResultBean deleteProject(@RequestBody String deleteProject){
        JSONObject jsonObject=JSON.parseObject(deleteProject);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.deleteProject(projectId);
    }



    /**
     * searchArrangementProject
     * 项目管理搜索查询项目信息
     * @param searchArrangementProject
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchArrangementProject",method = RequestMethod.POST)
    public ResultBean searchArrangementProject(@RequestBody String searchArrangementProject){
        JSONObject jsonObject=JSONObject.parseObject(searchArrangementProject);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer collegeId=jsonObject.getInteger("collegeId");
        Integer majorId=jsonObject.getInteger("majorId");
        String projectName=jsonObject.getString("projectName");
        return experimentService.searchArrangementProject(pageNum,pageSize,collegeId,majorId,projectName);
    }


    /**
     * 项目管理根据项目编号来判断这个实验是否已经被布置了
     * @param checkAssignStatus
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkAssignStatus",method = RequestMethod.POST)
    public ResultBean checkAssignStatus(@RequestBody String checkAssignStatus){
        JSONObject jsonObject=JSON.parseObject(checkAssignStatus);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.checkAssignStatus(projectId);
    }


    /**
     *查询这个项目被已经布置的教师信息
     * @param checkProjectAssign
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkProjectAssign",method =RequestMethod.POST)
    public ResultBean checkProjectAssign(@RequestBody String checkProjectAssign){
        JSONObject jsonObject=JSON.parseObject(checkProjectAssign);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.checkProjectAssign(projectId);
    }



    /**
     * 根据布置项目（所属学院,所属学科,项目级别,项目学时,项目负责人）
     * @param project
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/arrangementProject",method = RequestMethod.POST)
    public ResultBean arrangementProject(@RequestBody Project project){
        return experimentService.arrangementProject(project);
    }


    /**
     * 项目导出
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/exportProject",method = RequestMethod.GET)
    public void exportProject(HttpServletRequest request, HttpServletResponse response){
         experimentService.exportProject(request,response);
    }



    /**
     * 项目维护之有条件（项目名称）来查询项目信息
     * @param searchProjectMaintenance
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchProjectMaintenance",method = RequestMethod.POST)
    public ResultBean searchProjectMaintenance(@RequestBody String searchProjectMaintenance){
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
        JSONObject jsonObject=JSONObject.parseObject(searchProjectMaintenance);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String projectName=jsonObject.getString("projectName");
        return  experimentService.searchProjectMaintenance(pageNum,pageSize,projectName,sysUser.getUserId().intValue());
    }


    /**
     * 项目维护人根据项目编号来获取项目的详细信息
     * @param projectMaintenanceDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/getProjectMaintenanceDetail",method = RequestMethod.POST)
    public ResultBean getProjectMaintenanceDetail(@RequestBody String projectMaintenanceDetail){
        JSONObject jsonObject=JSONObject.parseObject(projectMaintenanceDetail);
        Integer projectId=jsonObject.getInteger("projectId");
        return  experimentService.getProjectMaintenanceDetail(projectId);
    }


    /**
     * 项目维护编辑基本信息
     * @param project
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editProjectMaintenance",method = RequestMethod.POST)
    public ResultBean editProjectMaintenance( Project project,MultipartFile file,Integer[] shareTeacherIds,Integer[] projectTeaIds){
        return experimentService.editProjectMaintenance(project,file,shareTeacherIds,projectTeaIds);
    }


    /**
     * 项目维护添加视频
     * @param video
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editProjectVideo",method = RequestMethod.POST)
    public ResultBean editProjectVideo(Video video,MultipartFile file){
        return experimentService.editProjectVideo(video,file);
    }

    /**
     * 项目维护根据项目编号来获取到项目的视频
     * @param getProjectVideo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProjectVideo",method = RequestMethod.POST)
    public ResultBean getProjectVideo(@RequestBody String getProjectVideo){
        JSONObject jsonObject=JSONObject.parseObject(getProjectVideo);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.getProjectVideo(projectId);
    }



    /**
     * 项目维护根据项目视频编号来删除视频
     * @param deleteProjectVideo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteProjectVideo",method = RequestMethod.POST)
    public ResultBean deleteProjectVideo(@RequestBody String deleteProjectVideo){
        JSONObject jsonObject=JSONObject.parseObject(deleteProjectVideo);
        Integer videoId=jsonObject.getInteger("videoId");
        return experimentService.deleteProjectVideo(videoId);
    }




    /**
     * 项目维护根据项目视频编号来修改视频
     * @param video
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateProjectVideo",method = RequestMethod.POST)
    public ResultBean updateProjectVideo(Video video,MultipartFile file){
        return experimentService.updateProjectVideo(video,file);
    }


    /**
     * 项目维护上传文件资料
     * @param projectId
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadProjectLearn",method = RequestMethod.POST)
    public ResultBean uploadProjectLearn(Integer projectId,MultipartFile file){
        LearnMaterials learnMaterials=new LearnMaterials();
        learnMaterials.setLearnProjectId(projectId);
//        learnMaterials.setMaterialsName(materialsName);
        return experimentService.uploadProjectLearn(learnMaterials,file);
    }

    /**
     *项目维护根据项目编号来获取到项目的文件资料（资料所属类型（1为视频，2为图片，3为文件资料））
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProjectLearnMaterials",method = RequestMethod.POST)
    public ResultBean getProjectLearnMaterials(@RequestBody String  getProjectLearnMaterials){
        JSONObject jsonObject=JSONObject.parseObject(getProjectLearnMaterials);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.getProjectLearnMaterials(projectId);
    }


    /**
     * 删除文件资料
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteProjectMaterials",method = RequestMethod.POST)
    public ResultBean deleteProjectMaterials(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer learnId=jsonObject.getInteger("learnId");
        return experimentService.deleteProjectMaterials(learnId);
    }



    /**
     * 项目维护根据文件资料编号来下载
     * @param request
     * @param response
     * @param learnId
     */
    @ResponseBody
    @RequestMapping(value = "/downloadProjectLearnMaterials",method = RequestMethod.GET)
    public void downloadProjectLearnMaterials(HttpServletRequest request, HttpServletResponse response,Integer learnId){
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
        experimentService.downloadProjectLearnMaterials(learnId,request,response);
    }


    /**
     * 项目维护修改实验报告和实验内容
     * @param project
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editProjectReport",method = RequestMethod.POST)
    public ResultBean editProjectReport(@RequestBody Project project){
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
        return experimentService.editProjectReport(project);

    }


    /**
     * 项目维护根据项目编号来获取项目的实验报告内容、实验报告模板
     * @param getProjectReport
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProjectReport",method = RequestMethod.POST)
    public ResultBean getProjectReport(@RequestBody String getProjectReport){
        SysUser sysUser= SecurityUtils.getLoginUser().getSysUser();
        JSONObject jsonObject=JSONObject.parseObject(getProjectReport);
        Integer projectId=jsonObject.getInteger("projectId");
        return experimentService.getProjectReport(projectId);
    }

}
